/*	This program produces Appendix Figure D.3 */

***** Set directories 
local dir_clean 	"~/Dropbox/Retirement gaming/clean"
local dir_output 	"~/Dropbox/Retirement gaming/output/dataverse"


use "`dir_clean'/extmargin_medbcw.dta", clear

local listcontrols1  = " i.sample_self_empl i.t i.t#i.sample_self_empl i.ndep_cat_1stobs i.ciiu2_1stob i.ndep_cat_1stobs#i.sample_self_empl i.ciiu2_1stobs#i.sample_self_empl i.year#i.ciiu2_1stobs i.year#i.ndep_cat_1stobs i.year#i.ciiu2_1stobs#i.sample_self_empl i.year#i.ndep_cat_1stobs#i.sample_self_empl" 
local listcontrols2  = " i.sample_emplsmall i.t i.t#i.sample_emplsmall i.ndep_cat_1stobs i.ciiu2_1stob i.ndep_cat_1stobs#i.sample_emplsmall i.ciiu2_1stobs#i.sample_emplsmall i.year#i.ciiu2_1stobs i.year#i.ndep_cat_1stobs i.year#i.ciiu2_1stobs#i.sample_emplsmall i.year#i.ndep_cat_1stobs#i.sample_emplsmall " 

* Outcome variable: registered and self-employed or employed
g regist_instatus = self_empl if sample_self_empl==1
replace regist_instatus = empl if  sample_self_empl==0

*** DID SELF-EMPLOYED VS. EMPLOYED LARGE FIRMS 
preserve
replace time_bcw=-5 if time_bcw==-6
keep if (sample_empllarge==1 )| (sample_self_empl==1 )
* Age dummies interacted with self_employed
tab time_bcw, gen(time_bcw_dums)
forvalues d = 1(1)13 {
	g did_edad`d' = time_bcw_dums`d'* sample_self_empl
}		
drop time_bcw_dums5 // drop year before ref age, so everything becomes relative to that
drop did_edad5
* Regression
reghdfe regist_instatus time_bcw_dums* did_edad* , absorb(`listcontrols1') vce(cluster i )
* Sample size
global n=e(N)
global N=e(N_clust1)
* Coefficients and sd
for any beta sd: gen X = .
forvalues d = 1(1)13 {
	capture qui replace beta = _b[did_edad`d'] if time_bcw==`d'-6
	capture qui replace sd   = _se[did_edad`d'] if time_bcw==`d'-6
} 
replace beta=0 if time_bcw==-1 
replace sd=0 if time_bcw==-1  
* Plot the results
collapse beta* sd*, by(time_bcw)
gen sd_top = beta + 1.96*sd	
gen sd_bot = beta - 1.96*sd
gen sd_top10 = beta + 1.645*sd	
gen sd_bot10 = beta - 1.645*sd
twoway rcap sd_top sd_bot time_bcw, ///
	cmissing(n) lwidth(thin) lcolor(navy) xsc(r(-5 7)) xlabel(-5(1)7)  xline(-1, lc(green))  ysc(r(-0.09 .09)) ylabel(-0.09(0.03).09,grid)  ///
	|| rcap sd_top10 sd_bot10 time_bcw, cmissing(n) lwidth(thin) lcolor(navy) xsc(r(-5 7)) ///
	|| scatter beta time_bcw, mcolor(navy) scheme(s1color) lcolor(navy) lpattern(solid) yline(0, lcolor(gs2)) ///
	title("Probability in Sample")  note("N=${n}, Individuals=${N}") ///
	legend(off) ytitle("Estimated Coefficients") xtitle("Years Relative to Predicted Start BCW")   
graph export "`dir_output'/figureD3a.png",  replace 	
restore



*** DID SMALL FIRM VS. EMPLOYED LARGE FIRMS -- REGISTERED IN STATUS 
preserve
replace time_bcw=-5 if time_bcw==-6
keep if (sample_empllarge==1 )| (sample_emplsmall==1 )
* Age dummies interacted with self_employed
tab time_bcw, gen(time_bcw_dums)
forvalues d = 1(1)13 {
	g did_edad`d' = time_bcw_dums`d'* sample_emplsmall
}		
drop time_bcw_dums5 // drop year before ref age, so everything becomes relative to that
drop did_edad5	
* Regression
reghdfe regist_instatus  time_bcw_dums* did_edad* , absorb(`listcontrols2') vce(cluster i )
* Sample size
global n=e(N)
global N=e(N_clust1)
* Coefficients and sd
for any beta sd: gen X = .
forvalues d = 1(1)13 {
	capture qui replace beta = _b[did_edad`d'] if time_bcw==`d'-6
	capture qui replace sd   = _se[did_edad`d'] if time_bcw==`d'-6
} 
replace beta=0 if time_bcw==-1 
replace sd=0 if time_bcw==-1  
* Plot the results
collapse beta* sd*, by(time_bcw)
gen sd_top = beta + 1.96*sd	
gen sd_bot = beta - 1.96*sd
gen sd_top10 = beta + 1.645*sd	
gen sd_bot10 = beta - 1.645*sd
twoway rcap sd_top sd_bot time_bcw, ///
	cmissing(n) lwidth(thin) lcolor(navy) xsc(r(-5 7)) xlabel(-5(1)7)  xline(-1, lc(green))  ysc(r(-0.09 .09)) ylabel(-0.09(0.03).09,grid)    ///
	|| rcap sd_top10 sd_bot10 time_bcw, cmissing(n) lwidth(thin) lcolor(navy) xsc(r(-5 7)) ///
	|| scatter beta time_bcw, mcolor(navy) scheme(s1color) lcolor(navy) lpattern(solid) yline(0, lcolor(gs2)) ///
	title("Probability in Sample")   note("N=${n}, Individuals=${N}") ///
	legend(off) ytitle("Estimated Coefficients") xtitle("Years Relative to Predicted Start BCW")   
graph export "`dir_output'/figureD3b.png",  replace 	
restore
	
   
clear all
exit
